﻿using Furion.DatabaseAccessor;
using Furion.DistributedIDGenerator;
using Furion.DynamicApiController;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Project5.Core.Entities;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project5.Application
{
    /// <summary>
    /// 测试生成连续guid222222222
    /// </summary>
    public class Testing1Service : IDynamicApiController
    {
        private readonly IRepository<Testing> _repository;
        private readonly IDistributedIDGenerator _idGenerator;

        public Testing1Service(IRepository<Testing> repository, IDistributedIDGenerator idGenerator)
        {
            _repository = repository;
            _idGenerator = idGenerator;
        }

        /// <summary>
        /// 测试生成连续guid1111111
        /// </summary>
        [HttpGet]
        public async Task GenerateGuid()
        {
            for (var i = 0; i < 1000; i++)
            {
                await _repository.InsertAsync(new Testing
                {
                    Id = (Guid)_idGenerator.Create(),
                    Sort = i,
                    CreateTime = DateTimeOffset.Now,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    Testing1 = new Testing1
                    {
                        Id = (Guid)_idGenerator.Create(),
                        Sort = i,
                        CreateTime = DateTimeOffset.Now,
                        Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                    }
                });
            }
        }

        public async Task<List<Testing>> GetList()
        {
            return await _repository.DetachedEntities.Include(t => t.Testing1).ToListAsync();
        }

        /// <summary>
        /// 测试批量删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task BulkDelete(int Sort)
        {
            var result = await _repository.Entities.Include(t => t.Testing1).Where(t => t.Sort > Sort).ExecuteDeleteAsync();
        }

        /// <summary>
        /// 测试批量更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task BulkUpdate()
        {
            var now = DateTimeOffset.Now;
            var result = await _repository.Entities.ExecuteUpdateAsync(s => s
            .SetProperty(b => b.CreateTime, b => now)
            );
        }
    }
}